<template>
  <a-modal
    title="嵌入子算法"
    :width="400"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <j-form-container>
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="标识名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="funId">
              <a-input v-model="model.funId" placeholder="请输入标识名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="算法名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="selectFormulaName">
              <!-- <j-select-formula
                placeholder="请选择算法"
                :multiple="false"
                :buttons="false"
                v-model="model.selectFormulaId"
              ></j-select-formula>  -->
              <a-input-search
                placeholder="选择算法"
                readOnly
                v-model="model.selectFormulaName"
                @search="doSelectFormula"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="获取结果" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-radio-group v-model="model.runResultType">
                <a-radio :value="1"> 运算结果 </a-radio>
                <a-radio :value="2"> 参考值 </a-radio></a-radio-group
              >
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
    <select-list-modal ref="selectListModal" @ok="selectOk" />
  </a-modal>
</template>

<script>
import JSelectFormula from '@/components/jeecgbiz/JSelectFormula'
import SelectListModal from '@/views/corp/userFormula/SelectList'
import { postAction } from '@/api/manage'
export default {
  name: 'insertFormula',
  components: { JSelectFormula, SelectListModal },
  data() {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      title: '操作',
      visible: false,
      model: {
        id: 0,
        funId: '',
        selectFormulaId: '', //选择的公式
        selectFormulaName: '', //选择的公式
        runResultType: 1,
        runResultTypeText: '',
      },
      oldFunId:'',//旧funId
      validatorRules: {
        selectFormulaName: [{ required: true, message: '请选择算法名称!' }],
        funId: [{ required: true, message: '请输入标识名称!' }],
      },
      confirmLoading: false,
      url: {
        addFormulaDetail: '/userFormula/userFormula/addFormulaDetail',
      },
    }
  },
  created() {},
  props: {
    formulaId: {
      type: Number,
    },
  },
  methods: {
    open() {
      this.model = {
        id: 0,
        funId: '',
        selectFormulaId: '', //选择的公式
        selectFormulaName: '', //选择的公式
        runResultType: 1,
      }
      this.visible = true
    },
    edit(record) {
      console.log('record：', record)
      this.model = Object.assign({}, JSON.parse(record.funParaJson))
      this.model.id = record.id
      this.oldFunId = this.model.funId;
      this.visible = true
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleCancel() {
      this.close()
    },
    handleSubmit() {
      var that = this
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.model.runResultTypeText = that.model.runResultType == 1 ? '运算结果' : '参考值'
          that.insertFormulaOk(that.model)
        } else {
          return false
        }
      })
    },
    insertFormulaOk(param) {
      this.confirmLoading = false
      var para = param.selectFormulaId + ',' + param.runResultTypeText
      var newRow = {
        id: param.id,
        formulaId: this.formulaId,
        funPara: para,
        funParaJson: JSON.stringify(param),
        funId: param.funId,
        funType: '嵌入子算法',
      }
      this.saveFunItem(newRow)
    },
    saveFunItem(param) {
      let that = this
      postAction(that.url.addFormulaDetail, param).then((res) => {
        if (res.success) {
          that.$message.success('保存成功')
          that.$emit('ok', {oldFunId:that.oldFunId, obj:this.model})
          that.close()
        } else {
          that.$message.warning(res.message)
        } 
        that.confirmLoading = false
      })
    },
    doSelectFormula() {
      this.$refs.selectListModal.show([this.formulaId])
    },
    selectOk(obj) { 
      this.model.selectFormulaId = obj.id
      this.model.selectFormulaName = obj.formulaName
      if (!this.model.funId) {//标识名称没赋值 默认赋 算法名称
        this.model.funId = obj.formulaName
      }
      this.$refs.form.validate();
    },
  },
}
</script>

<style>
</style>
